// Simple blinking led program for the LPC-H2148 board

/* *********************************************************
               Function declarations
  ********************************************************* */

void Initialize(void);
void feed(void);

void IRQ_Routine (void)   __attribute__ ((interrupt("IRQ")));
void FIQ_Routine (void)   __attribute__ ((interrupt("FIQ")));
void SWI_Routine (void)   __attribute__ ((interrupt("SWI")));
void UNDEF_Routine (void) __attribute__ ((interrupt("UNDEF")));

/**********************************************************
                  Header files
 **********************************************************/
#include "LPC2148.h"

/**********************************************************
                       MAIN
**********************************************************/

#define BUZZER		(0x01 << 25)

int	main (void) {
	
	int		j;										// loop counter (stack variable)

	
	// Initialize the system
	Initialize();		
	
	IO1DIR |= BUZZER;	
	IO1SET =  BUZZER;
 	PINSEL0 = 0x20000000;
 	VICIntSelect = 0x00008000;
 	VICIntEnable = 0x00008000;
	
	// endless loop to toggle the two leds
	while (1);
/*	
	 {
		
		for (j = 0; j < 500000; j++ );		// wait 500 msec
		IO1SET = BUZZER;
		for (j = 0; j < 500000; j++ );		// wait 500 msec
		IO1CLR = BUZZER;
			
	}
*/
}



/**********************************************************
                      Initialize
**********************************************************/

#define PLOCK 0x400

void Initialize(void)  {

   
	// Setting Multiplier and Divider values
  	PLL0CFG=0x24;
  	feed();
  
	// Enabling the PLL */	
	PLL0CON=0x1;
	feed();
  
	// Wait for the PLL to lock to set frequency
	while(!(PLL0STAT & PLOCK)) ;
  
	// Connect the PLL as the clock source
	PLL0CON=0x3;
	feed();
  
	// Enabling MAM and setting number of clocks used for Flash memory fetch (4 cclks in this case)
	MAMCR=0x2;
	MAMTIM=0x4;
  
	// Setting peripheral Clock (pclk) to System Clock (cclk)
	VPBDIV=0x1;

}


void feed(void)
{
  PLL0FEED=0xAA;
  PLL0FEED=0x55;
}

/*  Stubs for various interrupts (may be replaced later)  */
/*  ----------------------------------------------------  */

void IRQ_Routine (void) {
	while (1) ;	
}

void FIQ_Routine (void)  {
	
		IO1CLR = BUZZER;
	
}


void SWI_Routine (void)  {
	while (1) ;	
}


void UNDEF_Routine (void) {
	while (1) ;	
}
